package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.Question;
import cn.wolfcode.domain.QuestionData;
import cn.wolfcode.domain.QuestionItem;
import cn.wolfcode.enums.SubjectTitleEnum;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IQuestionItemService;
import cn.wolfcode.service.IQuestionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;
    @Autowired
    private IQuestionService questionService;
    @Autowired
    private IQuestionItemService questionItemService;


    @RequiredPermission(name = "试卷页面", expression = "exam:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Exam> pageInfo = examService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "exam/list";
    }


    @RequiredPermission(name = "试卷删除", expression = "exam:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            examService.delete(id);
        }
        return "redirect:/exam/list";
    }

//    @ResponseBody
//    @RequiredPermission(name = "试卷新增/编辑", expression = "exam:saveOrUpdate")
//    @RequestMapping("/saveOrUpdate")
//    public Exam saveOrUpdate(Exam exam) {
//        examService.save(exam);
//        return exam;
//    }


    //@RequiredPermission(name="试卷新增/编辑",expression="exam:saveOrUpdate")
    @RequestMapping("/input")
    public String input() {
        return "/exam/manage";
    }

    //@RequiredPermission(name="试卷新增/编辑",expression="exam:manage")
    @RequestMapping("/preview")
    public String manage(Exam exam, Model model) {
        examService.save(exam);
        Long examId = exam.getId();
//        Long examId1 = Long.parseLong(examId);
        model.addAttribute("exam", examService.get(examId));
        model.addAttribute("enums", SubjectTitleEnum.values());
        //查询所有题目和判断题答案
        List<Question> questions = questionService.selectQuestionAndJudgeAnswer(examId);
        model.addAttribute("questionAndJudgeAnswer", questions);
        //查出单选题和多选题选项和答案
        List<QuestionItem> questionItems = questionItemService.selectChoiceAndAnswer(examId);
        model.addAttribute("choiceAndAnswer", questionItems);
        //model.addAttribute("")
        return "exam/preview";
    }
}
